package com.yaozheng.vocationaltraining.service.impl;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;

import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.IBaseView;
import com.yaozheng.vocationaltraining.iview.IIntegralLotteryView;
import com.yaozheng.vocationaltraining.service.IntegralLotteryService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

@EBean
public class IntegralLotteryServiceImpl implements IntegralLotteryService {
	IIntegralLotteryView iIntegralLotteryView;

	@Override
	public void init(IIntegralLotteryView iIntegralLotteryView) {
		this.iIntegralLotteryView = iIntegralLotteryView;
	}

	@Background
	@Override
	public void lottery() {
		try {
			OkHttpClientManager.getInstance().get(
					Constants.ROOTURL + "/user/score/lottery",
					iIntegralLotteryView.getToken(),
					new OkHttpClientManager.IBaseViewCallback(new BaseView(iIntegralLotteryView) {

						@Override
						public void responseSuccess(JSONObject jsonObject) {
							iIntegralLotteryView.lotterySuccess(jsonObject);
						}

						@Override
						public void responseError(String error) {
							iIntegralLotteryView.lotteryError(error);
						}

					}));
		} catch (Exception e) {
			ErrorUtils.outErrorLog(e);
			if (iIntegralLotteryView.isResponseResult()) {
				iIntegralLotteryView
						.lotteryError(ErrorUtils.SERVER_CONNECTION_ERROR);
			}

		}
	}

}
